import _minimist from 'minimist'
import _path from 'path'
import _fs from 'fs-extra'
import _child_process, { ChildProcess } from 'child_process'


import { RawAsset } from './meta/RawAsset';
import { Folder } from './meta/Folder';
import { TypeMgr } from './TypeMgr';
import { Text } from './meta/Text';
import { Scene } from './meta/Scene';
import { TypeScript } from './meta/TypeScript';
import { Atlas } from './meta/Atlas';
import { Skel } from './meta/Skel';
import { Texture } from './meta/Texture';
import { UuidMgr } from './UuidMgr';
import { Plist } from './meta/PList';
import { Audio } from './meta/Audio';


interface FileRes {

    /** 扩展名 */
    exts: string[];

    /** 代码  */
    code: any;
}

class Main {

    /** 项目目录 */
    project: string = "";

    /** 检查生成目录 */
    build_path: string = '';

    /** 包含的文件,没有就是全部 */
    includeArr: string[] = [];

    /** 传入的参数 */
    argv: any;

    typeMgr: TypeMgr = new TypeMgr();


    constructor() {
        let mgr = this.typeMgr;
        mgr.register(Folder);
        mgr.register(Text);
        mgr.register(Scene);
        mgr.register(TypeScript);
        mgr.register(Atlas);
        mgr.register(Skel);
        mgr.register(Texture);
        mgr.register(Plist);
        mgr.register(Audio);
    }

    init() {
        let argv: any = this.argv = _minimist(process.argv.slice(2));
        console.log(argv);
        
        let thisObj = this as any;
        let keys = Object.keys(argv);
        for (const key of keys) {
            if (thisObj.hasOwnProperty(key)) {
                thisObj[key] = argv[key];
            }
        }

        if (argv.includes) {
            this.includeArr = String(argv.includes).split(',');
        }

        console.log('clientPath:', this.project);

        UuidMgr.walkDirGetMeta(this.project);

        console.log('get meta complite:', this.build_path);

        this.walkDirCheckMeta(this.build_path);
    }

    async chekMeta(checkPath: string) {

        
        //图片检查判断
        if (this.includeArr && this.includeArr.length) {
            let has = false;
            for (const iterator of this.includeArr) {
                if (checkPath.includes(iterator)) {
                    has = true;
                    break;
                }
            }

            if (!has) {
                return;
            }
        }

        let cls = this.typeMgr.getType(checkPath);
        if (!cls) {
            return;
        }

        let getObj = new cls(checkPath) as RawAsset;
        //等待检测是否删除
        let isExist = getObj.metaExist();
        if (isExist) {
            await getObj.check();
        }
        isExist = getObj.metaExist();
        if (isExist) {
            return;
        }

        //等待
        await getObj.importPath();

        let serialize = getObj.serialize();
        let ttt = JSON.stringify(serialize, null, 2);
        let metaPath = getObj.getMetaPath();
        _fs.writeFileSync(metaPath, ttt);
        console.log('生成meta文件:' + metaPath);
    }

    /**
     * 递归遍历,生成meta文件
     * @param dir 
     */
    walkDirCheckMeta(dir: string) {

        let files = _fs.readdirSync(dir);
        files.forEach((path: string) => {
            let ext = _path.extname(path);
            if (ext == '.meta') {
                return;
            }
            
            let fullPath = _path.join(dir, path);

            let isDirectory = _fs.statSync(fullPath).isDirectory();
            this.chekMeta(fullPath);

            if (isDirectory) {
                this.walkDirCheckMeta(fullPath);
            }
        })
    }
};

(new Main).init();